package com.example.houtai.service.impl;

import com.example.houtai.entity.SysRole;
import com.example.houtai.entity.SysRoleDept;
import com.example.houtai.entity.SysRoleMenu;
import com.example.houtai.mapper.SysRoleDeptMapper;
import com.example.houtai.mapper.SysRoleMapper;
import com.example.houtai.mapper.SysRoleMenuMapper;
import com.example.houtai.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.org.dto.RoleDto;
import com.org.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author 龚梦
 * @since 2021-03-22
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysRoleDeptMapper sysRoleDeptMapper;

    @Override
    public PageInfo<RoleDto> listAll(RoleDto roleDto) {
        PageHelper.startPage(roleDto.getPageNum(),roleDto.getPageSize());
        return new PageInfo<>(sysRoleMapper.listAll(roleDto));
    }

    @Override
    public ResultEntity insert(RoleDto roleDto) {
        try {
            SysRole sysRole=new SysRole();
            BeanUtils.copyProperties(roleDto,sysRole);
            sysRole.setCreateTime(new Date());
            sysRoleMapper.insert(sysRole);
            for(int i=0;i<roleDto.getMenuIds().length;i++){
                SysRoleMenu sysRoleMenu=new SysRoleMenu();
                sysRoleMenu.setRoleId(sysRole.getRoleId());
                sysRoleMenu.setMenuId(Long.parseLong(roleDto.getMenuIds()[i].toString()));
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
            SysRoleDept sysRoleDept=new SysRoleDept();
            sysRoleDept.setRoleId(sysRole.getRoleId());
            sysRoleDept.setDeptId(roleDto.getDeptId());
            sysRoleDeptMapper.insert(sysRoleDept);
            return ResultEntity.ok("200","新增角色成功",null);
        }catch (Exception e){
            log.error("method has failed", e);
            return ResultEntity.error("400","新增角色失败",null);
        }
    }

    @Override
    public ResultEntity edit(RoleDto roleDto) {
        System.out.println(roleDto);
        try {
            SysRole sysRole=new SysRole();
            BeanUtils.copyProperties(roleDto,sysRole);
            sysRole.setCreateTime(new Date());
            sysRoleMapper.updateRoleById(sysRole);

            //删除菜单中间表
            sysRoleMenuMapper.deleteByRoleId(roleDto.getRoleId());
            sysRoleDeptMapper.deleteByRoleId(roleDto.getRoleId());

            for(int i=0;i<roleDto.getMenuIds().length;i++){
                SysRoleMenu sysRoleMenu=new SysRoleMenu();
                sysRoleMenu.setRoleId(sysRole.getRoleId());
                sysRoleMenu.setMenuId(Long.parseLong(roleDto.getMenuIds()[i].toString()));
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
            SysRoleDept sysRoleDept=new SysRoleDept();
            sysRoleDept.setRoleId(sysRole.getRoleId());
            sysRoleDept.setDeptId(roleDto.getDeptId());
            sysRoleDeptMapper.insert(sysRoleDept);
            return ResultEntity.ok("200","编辑角色成功",null);
        }catch (Exception e){
            log.error("method has failed", e);
            return ResultEntity.error("400","编辑角色失败",null);
        }
    }

    @Override
    public ResultEntity delete(RoleDto roleDto) {
        try{
            if(roleDto.getRoleUsers().toString().indexOf("null")!=1){
                return ResultEntity.error("4001","不能删除拥有用户的角色",null);
            }
            sysRoleMapper.deleteById(roleDto.getRoleId());
            sysRoleMenuMapper.deleteByRoleId(roleDto.getRoleId());
            sysRoleDeptMapper.deleteByRoleId(roleDto.getRoleId());
            /*for (Integer deptId : roleDto.getDeptIds()) {
                sysRoleMenuMapper.deleteById(deptId);
            }
            for (Integer menuId : roleDto.getMenuIds()) {
                sysRoleDeptMapper.deleteById(menuId);
            }*/
            return ResultEntity.ok("200","删除角色成功",null);
        }catch (Exception e){
            return ResultEntity.error("400","删除角色失败",null);
        }
    }

    @Override
    public ResultEntity selRoleByDeptId(Integer deptId) {
        try {
            return ResultEntity.ok("200","检索部门角色成功",sysRoleDeptMapper.selRoleByDeptId(deptId));
        }catch (Exception e){
            return ResultEntity.ok("400","检索部门角色失败",null);
        }
    }
}
